/**
 *  @file  vsLog.h version 1.0 2007-11-20 
 *  @brief Define class LogInstance.
 *  Copyright (c) 2007 by @author <abitfree@gmail.com>
 */

#ifndef __VS_LOG_H__
#define __VS_LOG_H__

#include <list>
#include "vsLogger.h"

namespace vs {
namespace log {

////////////////////////////////
// Wrapper for Logger

/** 
@brief The wrapper for Logger, it provides the source file name and line number.
*/
class LogInstance
{
public:
	/// constructor.
	LogInstance(const char *pszFile, int nLine, Logger::E_LOG_LEVEL eLogLevel);

	/// destructor.
	~LogInstance();

	/// Write log messages, and append the source file name and line number at the end of the message.
	void operator()(Logger &logger, const char *pszFormat, ...);

private:
	// The source file name.
	const char *m_pszSrcFile;

	// The source file line number.
	const int m_nSrcLine;

	// The log level of the log message.
	Logger::E_LOG_LEVEL m_eLogLevel;
};

// Define the global log level macros for short.
//#define LOG_DEBUG	Logger::LOG_DEBUG
//#define LOG_INFO	Logger::LOG_INFO
//#define LOG_WARN	Logger::LOG_WARN
//#define LOG_ERROR	Logger::LOG_ERROR
//#define LOG_FATAL	Logger::LOG_FATAL

#define log_debug	Logger::LOG_DEBUG
#define log_info	Logger::LOG_INFO     
#define log_warn	Logger::LOG_WARN     
#define log_error	Logger::LOG_ERROR
#define log_fatal	Logger::LOG_FATAL


/////////////////////////////////////////////////////////////////////
// Define macros for logging.
/////////////////////////////////////////////////////////////////////

//#define VS_DEBUG	LogInstance(__FILE__, __LINE__, LOG_DEBUG)
//#define VS_INFO		LogInstance(__FILE__, __LINE__, LOG_INFO)
//#define VS_WARN		LogInstance(__FILE__, __LINE__, LOG_WARN)
//#define VS_ERROR	LogInstance(__FILE__, __LINE__, LOG_ERROR)
//#define VS_FATAL	LogInstance(__FILE__, __LINE__, LOG_FATAL)
#define VS_DEBUG	LogInstance(__FILE__, __LINE__, log_debug)
#define VS_INFO		LogInstance(__FILE__, __LINE__, log_info)
#define VS_WARN		LogInstance(__FILE__, __LINE__, log_warn)
#define VS_ERROR	LogInstance(__FILE__, __LINE__, log_error)
#define VS_FATAL	LogInstance(__FILE__, __LINE__, log_fatal)


#define INLINE inline

INLINE
LogInstance::LogInstance(const char *pszFile, int nLine, Logger::E_LOG_LEVEL eLogLevel)
	: m_pszSrcFile(pszFile), m_nSrcLine(nLine), m_eLogLevel(eLogLevel)
{
}

INLINE
LogInstance::~LogInstance()
{
}

INLINE
void LogInstance::operator()(Logger &logger, const char *pszFormat, ...)
{
	char szBuf[Logger::BUF_SIZE] = { 0 };

	va_list ap;
	va_start(ap, pszFormat);
	int nCount = vsprintf(szBuf, pszFormat, ap);
	va_end(ap);

	if (szBuf[nCount-1] == '\n')
	{
		szBuf[nCount-1] = '\0';
		logger.LogMsg(m_eLogLevel, "%s [%s:%d]\n", szBuf, (m_pszSrcFile), m_nSrcLine);
	}
	else
	{
		logger.LogMsg(m_eLogLevel, "%s [%s:%d]", szBuf, (m_pszSrcFile), m_nSrcLine);
	}
}

} // namespace log
} // namespace vs

#endif // __VS_LOG_H__
